<!DOCTYPE html>
<html>
<head>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="./resources/webgl-test.js"></script>
<script src="./resources/webgl-test-utils-full.js"></script>
</head>
<body>

<script id="myWorker" type="text/worker">
var canvas;
var gl;
var shouldGenerateGLError;
var WEBGL_lose_context;
var new_WEBGL_lose_context;
var allowRestore;
var contextLostEventFired;
var contextRestoredEventFired;
var OES_vertex_array_object;
var old_OES_vertex_array_object;
var OES_texture_float;
var newExtension;
// Purposely make all results true for easy testing
var results = [];

function compareGLError(glError, evalStr)
{
    var exception;
    try {
        eval(evalStr);
    } catch (e) {
        exception = e;
    }
    if (exception) {
        return false;
    } else {
        if (gl.getError() == glError)
            return true;
        return false;
    }
}

function setupTest()
{
    canvas = new OffscreenCanvas(10, 10);
    gl = canvas.getContext('webgl');
    WEBGL_lose_context = gl.getExtension("WEBGL_lose_context");
    if (!WEBGL_lose_context)
        return false;

    // Try to get a few extensions
    OES_vertex_array_object = gl.getExtension("OES_vertex_array_object");
    OES_texture_float = gl.getExtension("OES_texture_float");

    return true;
}

function reGetExtensionAndTestForProperty(gl, name, expectProperty) {
  newExtension = gl.getExtension(name);
  // NOTE: while getting a extension after context lost/restored is allowed to fail
  // for the purpose the conformance tests it is not.
  //
  // Hypothetically the user can switch GPUs live. For example on Windows, install 2 GPUs,
  // then in the control panen enable 1, disable the others and visa versa. Since the GPUs
  // have different capabilities one or the other may not support a particlar extension.
  //
  // But, for the purpose of the conformance tests the context is expected to restore
  // on the same GPU and therefore the extensions that succeeded previously should
  // succeed on restore.
  results.push(newExtension != null);
  if (expectProperty) {
    results.push(newExtension.webglTestProperty === true);
  } else {
    results.push(newExtension.webglTestProperty === undefined);
  }
  return newExtension;
}

function testLosingAndRestoringContext()
{
    if (!setupTest()) {
        results.push(false);
        postMessage(results);
    }

    canvas.addEventListener("webglcontextlost", function(e) {
        testLostContext(e);
        // restore the context after this event has exited.
        setTimeout(function() {
            results.push(compareGLError(gl.NO_ERROR, "WEBGL_lose_context.restoreContext()"));
            // The context should still be lost. It will not get restored until the
            // webglrestorecontext event is fired.
            results.push(gl.isContextLost());
            results.push(gl.getError() == gl.NO_ERROR);
            // gl methods should still be no-ops
            results.push(compareGLError(gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP)"));
        }, 0);
    });
    canvas.addEventListener("webglcontextrestored", function() {
        testRestoredContext();
        self.postMessage(results);
    });
    allowRestore = true;
    contextLostEventFired = false;
    contextRestoredEventFired = false;

    testOriginalContext();
    WEBGL_lose_context.loseContext();
    // The context should be lost immediately.
    results.push(gl.isContextLost());
    results.push(gl.getError() == gl.CONTEXT_LOST_WEBGL);
    results.push(gl.getError() == gl.NO_ERROR);
    // gl methods should be no-ops
    results.push(compareGLError(gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP)"));
    // but the event should not have been fired.
    results.push(!contextLostEventFired);
}

function testOriginalContext()
{
    results.push(!gl.isContextLost());
    results.push(gl.getError() == gl.NO_ERROR);
}

function testLostContext(e)
{
    results.push(!contextLostEventFired);
    contextLostEventFired = true;
    results.push(gl.isContextLost());
    results.push(gl.getError() == gl.NO_ERROR);
    if (allowRestore)
      e.preventDefault();
}

function testShouldNotRestoreContext(e)
{
    testFailed("Should not restore the context unless preventDefault is called on the context lost event");
}

function testOESTextureFloat() {
  if (OES_texture_float) {
    // Extension must still be lost.
    var tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    results.push(compareGLError(gl.INVALID_ENUM, "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.FLOAT, null)"));
    // Try re-enabling extension
    OES_texture_float = reGetExtensionAndTestForProperty(gl, "OES_texture_float", false);
    results.push(compareGLError(gl.NO_ERROR, "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.FLOAT, null)"));
  }
}

function testOESVertexArrayObject() {
  if (OES_vertex_array_object) {
    // Extension must still be lost.
    results.push(OES_vertex_array_object.createVertexArrayOES() == null);
    // Try re-enabling extension

    old_OES_vertex_array_object = OES_vertex_array_object;
    OES_vertex_array_object = reGetExtensionAndTestForProperty(gl, "OES_vertex_array_object", false);
    results.push(OES_vertex_array_object.createVertexArrayOES() != null);
    results.push(old_OES_vertex_array_object.createVertexArrayOES() == null);
  }
}

function testExtensions() {
  testOESTextureFloat();
  testOESVertexArrayObject();
}

function testRestoredContext()
{
    results.push(!contextRestoredEventFired);
    contextRestoredEventFired = true;
    results.push(!gl.isContextLost());
    results.push(gl.getError() == gl.NO_ERROR);
    testExtensions();
}
self.onmessage = function(e) {
    if (!setupTest()) {
        results.push(false);
        postMessage(results);
    }

    canvas.addEventListener("webglcontextlost", function(e) {
        testLostContext(e);
        // restore the context after this event has exited.
        setTimeout(function() {
            // we didn't call prevent default so we should not be able to restore the context
            results.push(compareGLError(gl.INVALID_OPERATION, "WEBGL_lose_context.restoreContext()"));
            testLosingAndRestoringContext();
        }, 0);
    });
    canvas.addEventListener("webglcontextrestored", testShouldNotRestoreContext);
    allowRestore = false;
    contextLostEventFired = false;
    contextRestoredEventFired = false;

    testOriginalContext();
    WEBGL_lose_context.loseContext();
    // The context should be lost immediately.
    results.push(gl.isContextLost());
    results.push(gl.getError() == gl.CONTEXT_LOST_WEBGL);
    results.push(gl.getError() == gl.NO_ERROR);
    // gl methods should be no-ops
    results.push(compareGLError(gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP)"));
    // but the event should not have been fired.
    results.push(!contextLostEventFired);
};

</script>

<script>
function makeWorker(script) {
   var blob = new Blob([script]);
   return new Worker(URL.createObjectURL(blob));
}

var t = async_test('Test that WebGL context restoration with OffscreenCanvas in a worker');
var worker = makeWorker(document.getElementById("myWorker").textContent);
worker.addEventListener('message', t.step_func_done(function(msg) {
    var results = msg.data;
    assert_greater_than(results.length, 0);
    for (var i = 0; i < results.length; i++)
        assert_equals(results[i], true);
}));
worker.postMessage("start");
</script>
</body>
</html>
